﻿using ERPSystem.Common.DTO;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using SalesManagementCommon.API.SalesManagementCommons.OutOfStock;

namespace SalesManagementCommon.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OutOfStockController : ControllerBase
    {

        private readonly IMediator _mediator;

        public OutOfStockController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 处理销售订单出库命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result<PageResultDto<OutOfStockaaawwwDTO>>> SerachOutOfStockCommand([FromQuery] OutOfStockQueryCommand request, CancellationToken cancellationToken)
        {
            return await _mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 销售出库反填
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result<OutOfStockDTO>> SerachOutOfStockBackCommand([FromQuery] OutOfStockBackCommand request, CancellationToken cancellationToken)
        {
            return await _mediator.Send(request, cancellationToken);
        }

        ///// <summary>
        ///// 销售出库导出Excel
        ///// </summary>
        ///// <returns></returns>
        //[HttpGet]
        //public IActionResult ExportExcel()
        //{
        //    byte[] data = null;
        //    string contentType = new FileExtensionContentTypeProvider().Mappings[".xlsx"];

        //    string fileName = "出库单.xlsx";

        //    IWorkbook workbook = new XSSFWorkbook();
        //    ISheet sheet = workbook.CreateSheet();
        //    IRow row = sheet.CreateRow(0);

        //    //设置表头
        //    row.CreateCell(0).SetCellValue("单据编码");
        //    row.CreateCell(1).SetCellValue("单据状态");
        //    row.CreateCell(2).SetCellValue("物料编码");
        //    row.CreateCell(3).SetCellValue("物料名称");
        //    row.CreateCell(4).SetCellValue("规格型号");
        //    row.CreateCell(5).SetCellValue("主计量");
        //    row.CreateCell(6).SetCellValue("数量");
        //    row.CreateCell(7).SetCellValue("单据日期");

        //    var list = 

        //    int rowIndex = 1;
        //    //设置数据
        //    foreach (var item in list)
        //    {
        //        IRow row1 = sheet.CreateRow(rowIndex);
        //        row.CreateCell(0).SetCellValue(item.DocumentNumber);
        //        row.CreateCell(1).SetCellValue(item.DocumentStatus);
        //        row.CreateCell(2).SetCellValue(item.ItemBianma);
        //        row.CreateCell(3).SetCellValue(item.MaterialName);
        //        row.CreateCell(4).SetCellValue(item.TypeSpecification);
        //        row.CreateCell(5).SetCellValue(item.WarehouseId);
        //        row.CreateCell(6).SetCellValue(item.OutboundQuantity);
        //        row.CreateCell(7).SetCellValue(item.DocumentDate);
        //        rowIndex++;
        //    }
        //    using (MemoryStream ms = new MemoryStream())
        //    {
        //        workbook.Write(ms);
        //        data = ms.ToArray();
        //    }
        //    return File(data, contentType, fileName);
        //}



    }
}
